﻿using HamcoWcs.Models.DbInfos;
using HamcoWcs.Models.PlcInfos;
using System.Collections.Generic;

namespace HamcoWcs.Models.Devices
{

    public class SrmPlc : MitsubishiPlc
    {
        public SrmPlc(string plcName, PlcInfoDto plcInfo, List<DbInfoDto> dbInfo) : base(plcName, plcInfo, dbInfo)
        {
        }

        public Dictionary<string, SrmDto> Srms { get; set; } = new Dictionary<string, SrmDto>();

        protected override void OnAfterDisconnect()
        {
            foreach (var srm in Srms.Values)
            {
                srm.DeviceMode = 0;
            }
        }
    }

    //public class SrmPlc1
    //{
    //    public MelsecMcNet McClient { get; set; } = new MelsecMcNet();
    //    public bool ConnectionStatus { get; set; }
    //    public string PlcName { get; }
    //    public PlcInfoDto SrmPlcInfo { get; set; } = new PlcInfoDto();
    //    public List<DbInfoDto> SrmDbInfo { get; set; } = new List<DbInfoDto>();

    //    public NodeInfoDto Aisle { get; set; }
    //    public int DeviceMode { get; set; }
    //    public int ErrorCode { get; set; }
    //    public int IsTask { get; set; }
    //    public int IsGoods { get; set; }
    //    public string UnitId { get; set; } = "";
    //    public int CurrentBay { get; set; }

    //    public string MachineName => SrmPlcInfo?.MachineName ?? "";
    //    public string PortName => SrmPlcInfo?.PortName ?? "";

    //    public int Enabled
    //    {
    //        get => enabled;
    //        set
    //        {
    //            bool isDiff = enabled != value;
    //            if (isDiff)
    //            {
    //                enabled = value;
    //                if (Aisle != null)
    //                    Aisle.CalcMode();
    //            }
    //        }
    //    }
    //    public int DestinationBay { get; set; }

    //    private int enabled;

    //    public string LastMsg = string.Empty;

    //    public DateTime LastMsgTime = DateTime.Now;

    //    public SrmPlc AnotherSrm { get; set; }


    //    public bool IsCanDo()
    //    {
    //        if (!ConnectionStatus) return false;
    //        if (Enabled == 0) return false;
    //        if (DeviceMode != 1) return false;
    //        if (IsTask == 1) return false;

    //        return true;
    //    }
    //    public bool IsRepeated(string msg)
    //    {
    //        var now = DateTime.Now;
    //        if (this.LastMsg == msg && (now - this.LastMsgTime).TotalSeconds <= GlobalValue.SRM_MSG_REPEAT_TIME)
    //            return true;
    //        this.LastMsg = msg;
    //        this.LastMsgTime = now;
    //        return false;
    //    }

    //    public SrmPlc1(string plcName)
    //    {
    //        PlcName = plcName;
    //    }

    //    public void Update(SrmRecvMsg srmRecvMsg)
    //    {
    //        DeviceMode = srmRecvMsg.DeviceMode;
    //        ErrorCode = srmRecvMsg.ErrorCode;
    //        IsTask = srmRecvMsg.IsTask;
    //        IsGoods = srmRecvMsg.IsGoods;
    //        UnitId = srmRecvMsg.UnitId;
    //        CurrentBay = srmRecvMsg.CurrentBay;
    //        Enabled = srmRecvMsg.Enabled;
    //        DestinationBay = srmRecvMsg.DestinationBay;
    //    }

    //    public bool ConnectToPlc()
    //    {
    //        if (this.SrmPlcInfo == null) throw new Exception($"{PlcName}无PLC信息");
    //        this.McClient.IpAddress = this.SrmPlcInfo.PlcIp;
    //        this.McClient.Port = int.Parse(this.SrmPlcInfo.PlcPort);
    //        var connectRe = this.McClient.ConnectServer();
    //        this.ConnectionStatus = connectRe.IsSuccess;
    //        return this.ConnectionStatus;
    //    }

    //    public void DisconnectToPlc()
    //    {
    //        this.McClient.ConnectClose();
    //        this.ConnectionStatus = false;
    //    }
    //}
}
